package com.all.play.myapplication.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.all.play.myapplication.R;


/**
 * Created by 聪明一只哈 on 2018/4/2.
 */

public class NumberSubAddView extends LinearLayout implements View.OnClickListener {

    private ImageView iv_sub;
    private ImageView iv_add;
    private TextView tv_value;

    private int num_min = 1;
    private int num_max = 100;

    private int value = 1;

    private Context context;

    public NumberSubAddView(Context context) {
        this(context, null);
    }

    public NumberSubAddView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
        this.context = context;
        View.inflate(context, R.layout.number_add_sub_layout, this);
        iv_add = (ImageView) findViewById(R.id.btn_add);
        iv_sub = (ImageView) findViewById(R.id.btn_sub);
        tv_value = (TextView) findViewById(R.id.tv_count);

        int value = getValue();
        tv_value.setText(value + "");

        iv_add.setOnClickListener(this);
        iv_sub.setOnClickListener(this);

    }

    public NumberSubAddView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public int getValue() {
        String vluestr = tv_value.getText().toString().trim();
        if (!TextUtils.isEmpty(vluestr) && vluestr != null) {
            value = Integer.parseInt(vluestr);
        }
        return value;
    }

    public void setValue(int value) {
        this.value = value;
        tv_value.setText(value + "");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_add:


                addNumber();

                break;
            case R.id.btn_sub:
                subNumber();

                break;

            default:

                break;
        }
        tv_value.setText(value + "");
       // Toast.makeText(context, "" + value, Toast.LENGTH_SHORT).show();

    }

    private void addNumber() {

        if (value < num_max) {
            value++;


        }
        setValue(value);
        if (onNumberChangeListener != null) {
            onNumberChangeListener.onChange(value);
        }
    }

    private void subNumber() {
        if (value > num_min) {
            value--;

        }
        setValue(value);
        if (onNumberChangeListener != null) {
            onNumberChangeListener.onChange(value);
        }

    }

    public int getNum_min() {
        return num_min;
    }

    public void setNum_min(int num_min) {
        this.num_min = num_min;
    }

    public int getNum_max() {
        return num_max;
    }

    public void setNum_max(int num_max) {
        this.num_max = num_max;
    }

    public interface OntvNumberChangeListener {
        void onChange(int value);
    }

    private OntvNumberChangeListener onNumberChangeListener;

    public void setOnNumberChangeListener(OntvNumberChangeListener onNumberChangeListener) {
        this.onNumberChangeListener = onNumberChangeListener;
    }
}
